package com.ksl.kinglockguard.network.appupdate;

import org.json.JSONException;
import org.json.JSONObject;

import com.lsk.open.core.MyLog;

public class AppUpdateInfo {
	public static final int DOWNLOAD_TYPE_NORMAL = 1;
	public static final int DOWNLOAD_TYPE_AUTO = 2;

	/**
	 * 更新类型
	 */
	public static final int UPDATE_TYPE_NORMAL_CODE = 1; // 普通更新
	public static final int UPDATE_TYPE_REC_CODE = 2; // 推荐更新
	public static final int UPDATE_TYPE_FORCE_CODE = 3; // 强制更新

	private int status;
	private long lastUpdate; // 该更新版本是何时上传到服务器的, 目前没用
	private String downloadUrl; // 下载地址
	private String versionName; // 新版本的版本名称
	// downloadType=1 表示"普通下载", 即需要弹出通知栏,提示用户发现更新,用户点击通知栏后开始下载.
	// downloadType=2 表示"静默下载", 即后台自动下载,不提示用户.
	private int updateType;
	private int downloadType;
	private String detail;
	private long packageSize;

	public long getPackageSize() {
		return packageSize;
	}

	public void setPackageSize(long packageSize) {
		this.packageSize = packageSize;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public long getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(long lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public String getDownloadUrl() {
		return downloadUrl;
	}

	public void setDownloadUrl(String downloadUrl) {
		this.downloadUrl = downloadUrl;
	}

	public String getVersionName() {
		return versionName;
	}

	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}

	public int getUpdateType() {
		return updateType;
	}

	public void setUpdateType(int updateType) {
		this.updateType = updateType;
	}

	public int getDownloadType() {
		return downloadType;
	}

	public void setDownloadType(int downloadType) {
		this.downloadType = downloadType;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public AppUpdateInfo(String jsonobj) throws JSONException {
		super();
		JSONObject response = new JSONObject(jsonobj);
		this.status = response.getInt("errno");
		boolean isNull = response.isNull("data");
MyLog.d("lijc", "AppUpdateInfo -----jsonobj: " + jsonobj + " ;\n isNull: " + isNull );
		if( !isNull){
			JSONObject data = response.getJSONObject("data");
			if(data != null){
				this.lastUpdate = data.getLong("upgradeable");
				this.downloadUrl = data.getString("url");
				this.versionName = data.getString("name");
				this.updateType = data.getInt("update_type");
				this.downloadType = data.getInt("download_type");
				this.detail = data.getString("digest");
				this.packageSize =  data.getLong("size");
MyLog.d("lijc", "AppUpdateInfo - --------- packageSize: " + packageSize );				
			}
		}
	}

	@Override
	public String toString() {
		return "AppUpdateInfo [status=" + status + ", lastUpdate=" + lastUpdate
				+ ", downloadUrl=" + downloadUrl + ", versionName="
				+ versionName + ", updateType=" + updateType
				+ ", downloadType=" + downloadType + ", detail=" + detail + "]";
	}
}
